<template>
  <div class="add-role">
    <main class="add-main">
      <div class="step-container">
        <el-steps direction="vertical" :active="activeStep">
          <el-step title="角色信息" />
          <el-step title="权限信息" />
          <el-step title="检查并完成" />
        </el-steps>
      </div>
      <div v-show="activeStep === 1" class="form-container">
        <div class="title">角色信息</div>
        <div class="form">
          <el-form
            class="form-box"
            :model="roleForm"
            :rules="roleRules"
            ref="roleform"
          >
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="roleForm.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.remark" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div v-show="activeStep === 2" class="form-container">
        <div class="title">权限配置</div>
        <div class="tree-wrapper">
          <div v-for="item in treeList" :key="item.id" class="tree-item">
            <div class="tree-title">{{ item.title }}</div>
            <el-tree
              ref="tree"
              :data="item.children"
              show-checkbox
              default-expand-all
              node-key="id"
              highlight-current
              :props="{ label: 'title' }"
            />
          </div>
        </div>
      </div>
      <div v-show="activeStep === 3" class="form-container">
        <div class="title">检查并完成</div>
        <div class="form">
          <div class="info">
            <div class="form-item">角色名称：{{ roleForm.roleName }}</div>
            <div class="form-item">角色描述：{{ roleForm.remark }}</div>
            <div class="form-item">角色权限：</div>
            <div class="tree-wrapper">
              <div v-for="item in diabledTree" :key="item.id" class="tree-item">
                <div class="tree-title">{{ item.title }}</div>
                <el-tree
                  ref="diabledTree"
                  :data="item.children"
                  show-checkbox
                  default-expand-all
                  node-key="id"
                  :highlight-current="false"
                  :props="{ label: 'title' }"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button v-if="activeStep > 1" @click="decreseStep">上一步</el-button>
        <el-button v-if="activeStep < 3" type="primary" @click="increseStep"
          >下一步</el-button
        >
        <el-button v-if="activeStep === 3" type="primary" @click="submit"
          >确认添加</el-button
        >
      </div>
    </footer>
  </div>
</template>

<script>
import { addroleuser, editroleuser, roledetail, rolelist } from "@/api/user";

export default {
  data() {
    return {
      activeStep: 1,
      roleForm: {
        perms: [],
      },
      diabledTree: [],
      treeList: [],
      roleRules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.gettree();
    if (this.$route.query) {
      this.getdetail(this.$route.query.roleId);
    }
  },
  methods: {
    async getdetail(roleid) {
      const res = await roledetail(roleid);
      console.log(res, "res");
      this.roleForm = res.data;
      this.$refs.tree.forEach((el, index) => {
        el.setCheckedKeys(this.roleForm.perms[index]);
      });
    },
    async submit() {
      if (this.$route.query) {
        delete this.roleForm.userTotal;
        const res = await editroleuser(this.roleForm);
        console.log(res);
        if (res.code == 10000) {
          this.$message.success("成功");
          this.$router.back();
        }
      } else {
        const res = await addroleuser(this.roleForm);
        console.log(res);
        if (res.code == 10000) {
          this.$message.success("成功");
          this.$router.back();
        }
      }
    },
    async gettree() {
      const res = await rolelist();
      console.log(res, "resss");
      this.treeList = res.data;
      this.diabledTree = JSON.parse(JSON.stringify(res.data));
      this.adddisable(this.diabledTree);
    },
    adddisable(list) {
      list.forEach((el) => {
        el.disabled = true;
        if (el.children) {
          this.adddisable(el.children);
        }
      });
    },
    decreseStep() {
      if (this.activeStep === 1) return;
      this.activeStep--;
    },
    increseStep() {
      if (this.activeStep === 3) return;
      if (this.activeStep === 1) {
        this.$refs.roleform.validate((valid) => {
          if (valid) {
            this.activeStep++;
          }
        });
      } else if (this.activeStep === 2) {
        this.roleForm.perms = [];
        this.$refs.tree.forEach((el) => {
          console.log(el.getCheckedKeys().length, "el");
          if (el.getCheckedKeys.length !== 0) {
            this.roleForm.perms.push(el.getCheckedKeys());
          }
        });
        console.log(this.roleForm.perms, "eee");
        if (this.roleForm.perms.flat().length == 0) {
          this.$message.warning("请至少选择一项权限");
          return;
        } else {
          this.$refs.diabledTree.forEach((el, index) => {
            el.setCheckedKeys(this.roleForm.perms[index]);
          });
          this.activeStep++;
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.add-role {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    position: fixed;
    top: 64px;
    bottom: 88px;
    width: 100%;
    overflow-y: auto;
    background: #f4f6f8;
    padding: 20px 230px;
    display: flex;
    .step-container {
      height: 400px;
      width: 200px;
    }
    .form-container {
      flex: 1;
      background-color: #fff;
      overflow-y: auto;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
        }
        .info {
          font-size: 14px;
          color: #666;
          .form-item {
            margin-bottom: 20px;
          }
        }
      }
      .form-box {
        width: 600px;
        display: flex;
        flex-direction: column;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
    z-index: 10001;
  }
  .tree-wrapper {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .tree-item {
      flex: 1;
      padding: 0px 4px;
      text-align: center;
      .tree-title {
        background: #f5f7fa;
        text-align: center;
        padding: 10px 0;
        margin-bottom: 12px;
      }
    }
  }
}
</style>
